package com.siberhus.tdfl.processor;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.siberhus.tdfl.DataFileLoaderException;
import com.siberhus.tdfl.Employee;
import com.siberhus.tdfl.FieldDataException;
import com.siberhus.tdfl.processor.AbstractDataFileProcessor;

public class LongRunningDfp extends AbstractDataFileProcessor<Employee>{

	private Log logger = LogFactory.getLog(EmployeeBeanWrapDfp.class);
	
	private int successCounter = 0;
	
	public int getSuccessCounter(){
		return successCounter;
	}
	
	@Override
	public void validateItem(Employee item, FieldDataException fde)
			throws Exception {
		if(item.getEmail()==null){
			fde.addError("email", "missing required field");
		}
		if(Thread.currentThread().interrupted()){
			//put some cleaning code here
			throw new DataFileLoaderException("STOP");
		}
	}
	
	@Override
	public void processItem(Employee item) throws Exception {
		try{
			Thread.sleep(1000); //assume this is the real processing
		}catch(InterruptedException e){}
		logger.info("Complete saving -> "+item);
		successCounter++;
	}
	
	@Override
	public void onItemError(Employee item, Exception exception) {
		logger.warn("Item error -> "+item);
	}
}
